<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class category_level_3 extends CI_Controller {
	
	public function search(){
		if($this->check_session()){
			$this->load->Model("category_level_3_DAO");
			$keyword = $_POST['keyword'];
			$data['category_list'] = $this->category_level_3_DAO->search($keyword);
			$this->parser->parse('table/category', $data);
		} else {
			$this->logout();
		}
    }
	
	public function get_info(){
		if($this->check_session()){
			$this->load->Model("category_level_3_DAO");
			$id = $_POST['id'];
			$returnData = $this->category_level_3_DAO->get($id);
			if($returnData == null){
				echo $returnData;
			} else {
				echo json_encode($returnData);
			}
		} else {
			$this->logout();
		}
    }
	
	public function get_map(){
		if($this->check_session()){
			if(isset($_POST) && isset($_POST['category_level_1_id']) && isset($_POST['category_level_2_id'])){
				$this->load->Model("map_category_DAO");
				$category_level_1_id = $_POST['category_level_1_id'];
				$category_level_2_id = $_POST['category_level_2_id'];
				$data['category_level_3_list'] = $this->map_category_DAO->get($category_level_1_id, $category_level_2_id);
				$this->parser->parse('option/category_level_3', $data);
			}
		} else {
			$this->logout();
		}
	}
	
	public function add(){
		if($this->check_session()){
			$this->load->Model("category_level_3_DAO");
			$this->load->Model("model/category_level_3_model");
			$category = new category_level_3_model();
			$category->vi_name = $_POST['vi_name'];
			$category->en_name = $_POST['en_name'];
			$category->description = $_POST['description'];
			echo $this->category_level_3_DAO->add($category);
		} else {
			$this->logout();
		}
    }
	
	public function update(){
		if($this->check_session()){
			$this->load->Model("category_level_3_DAO");
			echo $this->category_level_3_DAO->update($_POST['id'], $_POST['vi_name'], $_POST['en_name'], $_POST['description']);
		} else {
			$this->logout();
		}
	}
	
	public function delete(){
		if($this->check_session()){
			$this->load->Model("category_level_3_DAO");
			echo $this->category_level_3_DAO->delete($_POST['id']);
		} else {
			$this->logout();
		}
    }
	
	public function logout(){
		$this->session->unset_userdata('session_username');
		$this->session->unset_userdata('session_role');
		redirect('/', 'refresh');
    }
	
	public function check_session(){
		$session_username = $this->session->userdata('session_username');
		return isset($session_username) && $session_username != '';
	}
}
